﻿var util = (function ()
{
    return {
        HighLightClass: "highlight_item",

        trim: function (str)
        {
            if (str != null)
                return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '')
            else
                return "";
        },

        replaceAll: function (text, word, newWord)
        {
            if (text != undefined)
            {
                var textReplace = text.toString();
                while (textReplace.indexOf(word) != -1)
                    textReplace = textReplace.replace(word, newWord);
                return textReplace;
            }
            return text;
        },

        clone: function (obj)
        {
            if (obj == null || typeof (obj) != 'object')
                return obj;
            var temp = obj.constructor();
            for (var key in obj)
                temp[key] = util.clone(obj[key]);

            return temp;
        },

        ajaxSetup: function (obj, onSuccess, onFailed, ref)
        {

            if (util.isNull(onSuccess))
                onSuccess = null;

            if (util.isNull(onFailed))
                onFailed = null;

            if (util.isNull(ref))
                ref = "";

            $.ajaxSetup({
                type: obj.type,
                contentType: obj.contentType,
                dataType: obj.dataType,
                url: obj.domain + obj.service + "/" + obj.method,
                success: function (results)
                {

                    if (!util.isNull(onSuccess))
                    {
                        var current = this;
                        if (this.context != undefined)
                            current = this.context;
                        else
                        {
                            if (typeof (this) != "object")
                                current = this.toString();
                        }

                        if (results.d != undefined)
                            onSuccess(results.d, current)
                        else
                            onSuccess(results, current)
                    }
                },
                failure: onFailed,
                context: ref
            });
        },        
        isNull: function (obj)
        {
            if (typeof (obj) == undefined || obj == undefined || obj == null)
                return true;
            return false;
        },

        format: function (template, obj, nullable, prefix, what, dateFormat)
        {
            if (template != null && obj != undefined && obj != null)
            {
                if (prefix != undefined)
                    prefix = prefix + ".";
                else
                    prefix = "";

                for (var property in obj)
                {
                    if (obj.hasOwnProperty(property))
                    {
                        var propValue = obj[property];
                        var propName = prefix + property;

                        if (propValue instanceof Object)
                        {
                            template = util.format(template, propValue, property);
                        }
                        else
                        {
                            if (propValue == undefined)
                                propValue = "";

                            propValue = propValue.toString();
                            if ((propValue == null || propValue == "") && nullable)
                                propValue = "null";

                            propValue = util.formatDate(propValue);

                            if (propValue == "")
                                propValue = " Desconocido ";

                            if (what != undefined && what != "")
                            {
                                propValue = util.highlight(propValue, what);
                            }

                            template = util.replaceAll(template, "{" + propName + "}", propValue);
                            template = util.replaceAll(template, "%7B" + propName + "%7D", propValue);
                        }
                    }
                }
            }

            return template;
        },

        highlight: function (value, what)
        {
            var whatNormalize = util.normalize(what.toLowerCase());
            var normalize = util.normalize(value.toLowerCase());
            var index = normalize.indexOf(whatNormalize);

            if (index >= 0)
            {
                value = util.insert(value, index + what.length, "</b>");
                value = util.insert(value, index, "<b>");
            }
            return value;
        },

        insert: function (str, index, string)
        {
            if (index > 0)
                return str.substring(0, index) + string + str.substring(index, str.length);
            else
                return string + str;
        },

        normalize: function (str)
        {
            return util.replaceAll(util.replaceAll(util.replaceAll(util.replaceAll(util.replaceAll(util.replaceAll(util.trim(str).toLowerCase(), "á", "a"), "é", "e"), "í", "i"), "ó", "o"), "ú", "u"), "\\", "/");
        },

        getTemplateHtml: function (templateHtml, obj, selector)
        {
            if (templateHtml == '')
            {
                templateHtml = $("#" + obj.Id + obj.Namespace + selector).html();

                if (templateHtml == null)
                {
                    templateHtml = '';
                }
                else
                {
                    templateHtml = util.trim(templateHtml);
                    templateHtml = util.replaceAll(templateHtml, "{style}", "style");
                }
            }

            return templateHtml;
        },

        formatDate: function (date)
        {
            if (date.indexOf("new Date(") == 0)
            {
                var d = eval(date);
                var day = util.formatDigits(d.getDate());
                var month = util.formatDigits(d.getMonth() + 1);
                var year = util.formatDigits(d.getFullYear());

                if (year > 1900)
                {
                    date = month + "/" + day + "/" + year;
                }
                else
                {
                    date = "";
                }
            }

            return date;
        },

        formatDigits: function (val)
        {
            if (val < 10)
                return "0" + val;
            return val;
        },

        find: function (lstData, filter, prop)
        {

            if (lstData != null)
            {
                var len = lstData.length;
                for (var i = 0; i < len; i++)
                {
                    if (lstData[i][prop] != null && lstData[i][prop].toString() == filter.toString())
                        return lstData[i];
                }
            }
            return null;
        },

        findAll: function (lstData, prop, filter)
        {
            if (filter != undefined)
            {
                filter = filter.toLowerCase();
                var results = new Array();
                if (lstData != null)
                {
                    var len = lstData.length;
                    for (var i = 0; i < len; i++)
                    {

                        if (lstData[i][prop] != null && lstData[i][prop].toString().toLowerCase().indexOf(filter) >= 0)
                        {
                            results.push(lstData[i]);
                        }
                    }
                }
                return results;
            }
            else
            {
                return lstData;
            }
        },

        exist: function (lstData, filter)
        {

            if (filter != undefined)
            {
                if (lstData != null)
                {
                    var len = lstData.length;
                    for (var i = 0; i < len; i++)
                    {
                        if (lstData[i] != null && lstData[i].toString() == filter.toString())
                            return true;
                    }
                }
            }
            return false;
        }
    }
}
)();
